package com.macrosolution.exdebito

import org.apache.commons.io.FileUtils;
import org.apache.ivy.util.FileUtil;


/**
 * FileDocument
 * A domain class describes the data object and it's mapping to the database
 */
class FileDocument {

	/* Default (injected) attributes of GORM */
//	Long	id
//	Long	version
	User user
	String fileName
	String filePath
	DocType documentType
	String file_estension
	Date	dateCreated
	Date	lastUpdated
	Long fileSize
	String comments
		
	

	
//	static	belongsTo	= []	// tells GORM to cascade commands: e.g., delete this object if the "parent" is deleted.
//	static	hasOne		= []	// tells GORM to associate another domain object as an owner in a 1-1 mapping
//	static	hasMany		= []	// tells GORM to associate other domain objects for a 1-n or n-m mapping
//	static	mappedBy	= []	// specifies which property should be used in a mapping 
	
    static	mapping = {
		
    }
    
	static	constraints = {
		comments blank:true
		documentType nullable:true
    }
	
	
	def afterDelete() {
		//put logic for delete file
		def path = this.getPathFile()
		
		File erasedFile = new File(path)
		
		if(erasedFile.exists() ){
			if(erasedFile.delete() ){
				
			}
		}
	}
	
	def getPathFile(){
		return this.filePath + File.separator + this.fileName
	}
	/*
	 * Methods of the Domain Class
	 */
//	@Override	// Override toString for a nicer / more descriptive UI 
//	public String toString() {
//		return "${name}";
//	}
	public String byteFormat() {
		return FileUtils.byteCountToDisplaySize(this.fileSize)
	}
}
